package com.rjkj.medicalwaste.adapter

import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.lifecycle.LifecycleOwner
import androidx.paging.AsyncPagingDataDiffer
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.rjkj.medicalwaste.R
import com.rjkj.medicalwaste.bean.*
import com.rjkj.medicalwaste.databinding.ItemBoxListBinding
import com.rjkj.medicalwaste.databinding.ItemTempBoxListBinding
import kotlin.math.log

class TempBoxListAdapter(private val context: Context, private val itemClick: ItemClick<GarbageInTempBoxBean>, var list: List<GarbageInTempBoxBean>):RecyclerView.Adapter<TempBoxListAdapter.ViewHolder>() {
    companion object {
        private val COMPARATOR = object : DiffUtil.ItemCallback<GarbageInTempBoxBean>() {
            override fun areItemsTheSame(oldItem: GarbageInTempBoxBean, newItem: GarbageInTempBoxBean): Boolean {
                return oldItem.tempBoxQRCode == newItem.tempBoxQRCode
            }

            override fun areContentsTheSame(oldItem: GarbageInTempBoxBean, newItem: GarbageInTempBoxBean): Boolean {
                return oldItem == newItem
            }
        }
    }

    class ViewHolder(private val binding:ItemTempBoxListBinding) : RecyclerView.ViewHolder(binding.root) {
        fun bind(context: Context,item:GarbageInTempBoxBean, itemClick: ItemClick<GarbageInTempBoxBean>){
            binding.boxData = item
            item.garbageType.let {
                if(it.colorId!=0){
                    binding.tvType.setTextColor(ContextCompat.getColor(context, it.colorId))
                }
                binding.tvType.text = it.typeName
            }
            binding.tvContent.text = item.garbageList.size.toString()
            binding.root.setOnClickListener {
                itemClick.onClicked(it,item)
            }
            binding.executePendingBindings()
        }
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val test = list[position]
        holder.bind(context,test,itemClick)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = ItemTempBoxListBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false)
        return ViewHolder(binding)
    }

    override fun getItemCount(): Int {
        return list.size
    }
}